﻿using System.Collections.Generic;
using System.ServiceModel.Activation;
using System.Text.RegularExpressions;
using Microsoft.SharePoint;

namespace Winsmarts.CrossSCNav.Services
{
    // NOTE: If you change the class name "LinksService" here, you must also update the reference to "LinksService" in App.config.
    [AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Required)]
    public class LinksService : ILinksService
    {
        #region ILinksService Members

        public List<BO.SiteCollectionLink> GetSiteCollections(string urlMatch)
        {
            List<BO.SiteCollectionLink> toReturn = new List<BO.SiteCollectionLink>();

            foreach (SPSite site in SPContext.Current.Site.WebApplication.Sites)
            {
                if (site.RootWeb.DoesUserHavePermissions(SPBasePermissions.ViewPages))
                {
                    MatchCollection matches = Regex.Matches(site.Url, urlMatch, RegexOptions.IgnoreCase);
                    if (matches.Count != 0)
                    {
                        toReturn.Add(new BO.SiteCollectionLink
                        {
                            SiteCollectionTitle = site.RootWeb.Title,
                            SiteCollectionUrl = site.Url
                        });
                    }
                }
                site.Dispose();
            }
            return toReturn;
        }
        #endregion
    }
}
